package com.jilefojing.util;

import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.jilefojing.beans.constant.CookieConstant;
import com.jilefojing.context.beans.vo.RememberMe;
import com.jilefojing.context.beans.vo.UserToken;

/**
 * 
 * @author jiangguoshun
 *
 */
public class CookieUtil {

	private static final Logger logger = LoggerFactory.getLogger(CookieUtil.class);
	private static final String DOMAIN = "";

	/**
	 * 设置cookie
	 * 
	 * @param name
	 * @param value
	 * @param domain
	 * @param expiry
	 * @param isHttpOnly
	 * @param response
	 */
	public static void setCookie(String name, String value, String domain, int expiry, boolean isHttpOnly, HttpServletResponse response) {
		Cookie cookie = new Cookie(name, value);
		// cookie.setDomain(domain);
		cookie.setMaxAge(expiry);
		cookie.setPath("/");
		cookie.setHttpOnly(isHttpOnly);
		response.addCookie(cookie);
	}

	/**
	 * 设置cookie
	 * 
	 * @param name
	 * @param value
	 * @param domain
	 * @param expiry
	 * @param response
	 */
	public static void setCookie(String name, String value, String domain, int expiry, HttpServletResponse response) {
		Cookie cookie = new Cookie(name, value);
		// cookie.setDomain(domain);
		cookie.setMaxAge(expiry);
		cookie.setPath("/");
		response.addCookie(cookie);

	}

	/**
	 * cookie永不过期
	 * 
	 * @param name
	 * @param value
	 * @param response
	 */
	public static void setCookie(String name, String value, HttpServletResponse response) {
		setCookie(name, value, DOMAIN, -1, response);
	}

	/**
	 * 设置过期
	 * 
	 * @param name
	 * @param value
	 * @param expiry
	 * @param response
	 */
	public static void setCookie(String name, String value, int expiry, HttpServletResponse response) {
		setCookie(name, value, DOMAIN, expiry, response);
	}

	/**
	 * 设置域名
	 * 
	 * @param name
	 * @param value
	 * @param domain
	 * @param response
	 */
	public static void setCookie(String name, String value, String domain, HttpServletResponse response) {
		setCookie(name, value, domain, -1, response);
	}

	/**
	 * 返回cookie给客户端
	 * 
	 * @param cookie
	 * @param response
	 */
	public static void setCookie(Cookie cookie, HttpServletResponse response) {
		response.addCookie(cookie);
	}

	/**
	 * 获取全部cookie
	 * 
	 * @param name
	 * @param request
	 * @return
	 */
	public static Cookie getCookie(String name, HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (name.equals(cookies[i].getName())) {
					return cookies[i];
				}
			}
		}
		return null;
	}

	/**
	 * 获取cookie值
	 * 
	 * @param name
	 * @param request
	 * @return
	 */
	public static String getValue(String name, HttpServletRequest request) {
		Cookie ck = getCookie(name, request);
		return null != ck ? ck.getValue() : null;
	}

	/**
	 * 清除所有cookie
	 * 
	 * @param request
	 * @param response
	 */
	public static void clearCookie(HttpServletRequest request, HttpServletResponse response) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = new Cookie(cookies[i].getName(), null);
				cookie.setPath("/");
				cookie.setMaxAge(0);
				// cookie.setDomain(DOMAIN);
				response.addCookie(cookie);
			}
		}
	}

	/**
	 * 添加cookie
	 * 
	 * @param name
	 *            cookie的key
	 * @param value
	 *            cookie的value
	 * @param domain
	 *            domain
	 * @param path
	 *            path
	 * @param maxage
	 *            最长存活时间 单位为秒
	 * @param response
	 */
	public static void addCookie(String name, String value, String domain, int maxage, String path, HttpServletResponse response) {
		Cookie cookie = new Cookie(name, value);
		if (domain != null) {
			cookie.setDomain(domain);
		}
		cookie.setMaxAge(maxage);
		cookie.setPath(path);
		response.addCookie(cookie);
	}

	/**
	 * 往根下面存一个cookie * @param name cookie的key
	 * 
	 * @param value
	 *            cookie的value
	 * @param domain
	 *            domain
	 * @param maxage
	 *            最长存活时间 单位为秒
	 * @param response
	 */
	public static void addCookie(String name, String value, String domain, int maxage, HttpServletResponse response) {
		addCookie(name, value, domain, maxage, "/", response);
	}

	/**
	 * 从cookie值返回cookie值，如果没有返回 null
	 * 
	 * @param req
	 * @param name
	 * @return cookie的值
	 */
	public static String getCookie(HttpServletRequest request, String name) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null)
			return null;
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(name)) {
				return cookies[i].getValue();
			}
		}
		return null;
	}

	public static void removeCookie(String name, String domain, HttpServletRequest request, HttpServletResponse response) {
		String cookieVal = getCookie(request, name);
		if (cookieVal != null) {
			CookieUtil.addCookie(name, null, domain, 0, response);
		}
	}

	public static void removeCookie(String name, HttpServletRequest request, HttpServletResponse response) {
		CookieUtil.removeCookie(name, ".dhgate.com", request, response);
	}

	public static UserToken getUserToken(HttpServletRequest request) {
		UserToken userToken = new UserToken();
		String time = CookieUtil.getCookie(request, CookieConstant.CURRENT_USER_TIME);
		String token = CookieUtil.getCookie(request, CookieConstant.CURRENT_USER_TOKEN);
		String sign = CookieUtil.getCookie(request, CookieConstant.CURRENT_USER_SIGN);
		String uid = CookieUtil.getCookie(request, CookieConstant.CURRENT_USER_UID);
		String rm = CookieUtil.getCookie(request, CookieConstant.CURRENT_USER_RM);

		userToken.setSign(sign);
		if (Detect.notEmpty(time)) {
			Date creatDate = new Date();
			creatDate.setTime(Long.valueOf(time));
			userToken.setTime(creatDate);
		}
		userToken.setToken(token);
		userToken.setUid(uid);
		if (Detect.notEmpty(rm)) {
			try {
				RememberMe rememberMe = JSONObject.parseObject(AESCoder.defaultDecrypt(rm), RememberMe.class);
				userToken.setRm(rememberMe);
			} catch (Exception e) {
				logger.error("RM解密异常:" + e.getMessage(), e);
			}
		}
		return userToken;
	}

	public static void writeUserToken(UserToken userToken, HttpServletResponse response) {
		// 31536000
		CookieUtil.setCookie(CookieConstant.CURRENT_USER_TIME, String.valueOf(userToken.getTime().getTime()), null, UserTokenUtil.USER_TOKEN_COOKIE_LIFE_SECONDS, true, response);
		CookieUtil.setCookie(CookieConstant.CURRENT_USER_TOKEN, userToken.getToken(), null, UserTokenUtil.USER_TOKEN_COOKIE_LIFE_SECONDS, true, response);
		CookieUtil.setCookie(CookieConstant.CURRENT_USER_UID, userToken.getUid(), null, UserTokenUtil.USER_TOKEN_COOKIE_LIFE_SECONDS, true, response);
		CookieUtil.setCookie(CookieConstant.CURRENT_USER_SIGN, userToken.getSign(), null, UserTokenUtil.USER_TOKEN_COOKIE_LIFE_SECONDS, true, response);
		if (userToken.getRm() != null) {
			try {
				CookieUtil.setCookie(CookieConstant.CURRENT_USER_RM, AESCoder.defaultEncrypt(JSONObject.toJSONString(userToken.getRm())), null, UserTokenUtil.USER_TOKEN_COOKIE_LIFE_SECONDS, true,
						response);
			} catch (Exception e) {
				logger.error("RM加密异常:" + e.getMessage(), e);
			}
		}
	}

	public static void clearUserToken(HttpServletRequest request, HttpServletResponse response) {
		Cookie[] cookies = request.getCookies();
		if (Detect.notEmpty(cookies)) {
			for (int i = 0; i < cookies.length; i++) {
				String _name = cookies[i].getName();
				if (_name.equals(CookieConstant.CURRENT_USER_SIGN) || _name.equals(CookieConstant.CURRENT_USER_TIME) || _name.equals(CookieConstant.CURRENT_USER_TOKEN)
						|| _name.equals(CookieConstant.CURRENT_USER_UID) || _name.equals(CookieConstant.CURRENT_USER_RM)) {
					Cookie cookie = new Cookie(_name, null);
					cookie.setPath("/");
					cookie.setMaxAge(0);
					// cookie.setDomain(DOMAIN);
					response.addCookie(cookie);
				}
			}
		}
	}
}
